Ext.define('iMedproplus.controller.admin.OrganizationStructureTree', {
    extend: 'Ext.app.Controller',
    alias: 'controller.organizationstructurecontroller',

    stores: [
    'iMedproplus.store.OrganizationStructure',
    'iMedproplus.store.StructureTree',
    ],
    
    views: [
    'admin.structure.UnitPanelGrid',
    'admin.structure.TreePanel'
    ],
    
    models:[
    'OrganizationStructure'
    ],
    
    selectedOrganizationStructure : [],
    selectedTreeNode : [],
    allowAddAjaxRequestResult: false,
    
    init: function() {
        this.control({
            'adminstrucutrepanelgrid':{
                organizationStructureSelectionChange: this.onOrganizationStructureSelectionChange,
                addOrganizationStrucutreToTree: this.onAllowAddOrganizationStrucutreToTree
            },
            'adminstructuretreepanel':{
                structureNodeSelectionChange: this.onStructureNodeSelectionChange,
                removeOrganizationStructureFromTree: this.onRemoveOrganizationStructureFromTree
            }
        });
    },
    
    onAddOrgStrucutreElemToTree: function(){
        if(!this.allowAddAjaxRequestResult){
            Ext.Msg.alert('Uwaga', 'Nie można dodać elementu: '
                +this.selectedOrganizationStructure[0].data.name+
                ' do '+this.selectedTreeNode[0].data.text+'.');
            return;
        }
        this.allowAddAjaxRequestResult = false;
        this.isAllowAddAjaxRequestActive = false;
        var targetNode = this.selectedTreeNode[0];
        targetNode.appendChild({
            text: this.selectedOrganizationStructure[0].data.name, 
            leaf: true,
            id: this.selectedOrganizationStructure[0].data.id
        });
        targetNode.data.leaf = false;
        targetNode.expand();
        this.getIMedproplusStoreStructureTreeStore().sync();
    },
    
    onAllowAddOrganizationStrucutreToTree: function(){
        if(!this.checkSelectedElements()){
            return;
        }
        Ext.Ajax.request({
            url: '/imedproplus-rest/rest/json/organizationstructure/tree/allowadd/get',
            params: {
                childId: this.selectedOrganizationStructure[0].data.id,
                parentId: this.selectedTreeNode[0].data.id
            },
            method : 'GET',
            scope : this,
            success: function(response){
                var responseObject = Ext.decode(response.responseText);
                if(responseObject.data.allowAddElementToHierarchy){
                    this.allowAddAjaxRequestResult = true;
                }else{
                    this.allowAddAjaxRequestResult = false;
                }
                this.onAddOrgStrucutreElemToTree();
            },
            failure: function(response){
                Ext.Msg.alert('Uwaga', 'Błąd aplikacji: '+response.message+'.');    
            }
        });
    },
    
    checkSelectedElements: function(){
        if(this.selectedOrganizationStructure.length==0){
            Ext.Msg.alert('Uwaga', 'Proszę wybrać jednostkę organizacyjną do dodania.');
            return false;
        }
        if(this.selectedOrganizationStructure.length!=1){
            Ext.Msg.alert('Uwaga', 'Proszę wybrać tylko jedną wybrać jednostkę organizacyjną do dodania.');
            return false;
        }
        if(this.selectedTreeNode.length==0){
            Ext.Msg.alert('Uwaga', 'Proszę wybrać element struktury organizacyjnej, do którego chcesz dodać.');
        }
        return true;
    },
    
    onOrganizationStructureSelectionChange: function(selection){
        this.selectedOrganizationStructure = selection;
    },
    
    onStructureNodeSelectionChange: function(selection){
        this.selectedTreeNode = selection;
    },
    
    onRemoveOrganizationStructureFromTree: function(){
        Ext.Msg.confirm('Uwaga', 'Czy na pewno chcesz usunąć element: '
            +this.selectedTreeNode[0].data.name+' z hierarchi?', function(btn){
                if (btn == 'yes'){
                    this.selectedTreeNode[0].remove();
                    this.getIMedproplusStoreStructureTreeStore().sync();
                }
            }, this);
    }

});